#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    m_model = new QStandardItemModel(this);
    ui->treeView->setModel(m_model);

    setupModel();
    setupConnections();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::setupModel()
{
    // 一级车
    QStandardItem *car1 = new QStandardItem("列车A");
    car1->setCheckable(true);
    m_model->appendRow(car1);

    // 二级功能组
    QStandardItem *group1_car1 = new QStandardItem("功能组A1");
    group1_car1->setCheckable(true);
    car1->appendRow(group1_car1);

    QStandardItem *group2_car1 = new QStandardItem("功能组A2");
    group2_car1->setCheckable(true);
    car1->appendRow(group2_car1);

    // 三级图纸
    QStandardItem *drawing1_g1_c1 = new QStandardItem("图纸A1-1");
    drawing1_g1_c1->setCheckable(true);
    group1_car1->appendRow(drawing1_g1_c1);

    QStandardItem *drawing2_g1_c1 = new QStandardItem("图纸A1-2");
    drawing2_g1_c1->setCheckable(true);
    group1_car1->appendRow(drawing2_g1_c1);

    QStandardItem *drawing3_g2_c1 = new QStandardItem("图纸A2-1");
    drawing3_g2_c1->setCheckable(true);
    group2_car1->appendRow(drawing3_g2_c1);

    // 可以在这里添加更多数据...
    ui->treeView->expandAll();//展开全部
}

void MainWindow::setupConnections()
{
    connect(m_model, &QStandardItemModel::itemChanged, this, [&](QStandardItem *item) {
        if (!item->isCheckable()) return; // 确保只有带复选框的项才处理

        // 获取当前项的复选框状态
        Qt::CheckState checkState = item->checkState();

        // 递归处理子项：如果父项被选中，所有子项也选中
        updateChildrenCheckState(item, checkState);

        // 递归处理父项：根据子项的状态更新父项的状态
        updateParentCheckState(item);
    });
}

void MainWindow::updateParentCheckState(QStandardItem *item)
{
    QStandardItem *parent = item->parent();
    if (!parent || !parent->isCheckable()) return;

    int checkedCount = 0;
    int totalCount = parent->rowCount();
    for (int row = 0; row < totalCount; ++row) {
        QStandardItem *child = parent->child(row);
        if (child && child->isCheckable()) {
            if (child->checkState() == Qt::Checked) {
                checkedCount++;
            } else if (child->checkState() == Qt::PartiallyChecked) {
                // 如果子项是部分选中，那么父项也必须是部分选中
                parent->setCheckState(Qt::PartiallyChecked);
                updateParentCheckState(parent);
                return;
            }
        }
    }

    if (checkedCount == totalCount) {
        parent->setCheckState(Qt::Checked);
    } else if (checkedCount == 0) {
        parent->setCheckState(Qt::Unchecked);
    } else {
        parent->setCheckState(Qt::PartiallyChecked);
    }

    // 继续更新上一级父项
    updateParentCheckState(parent);
}

void MainWindow::updateChildrenCheckState(QStandardItem *item, Qt::CheckState state)
{
    for (int row = 0; row < item->rowCount(); ++row) {
        QStandardItem *child = item->child(row);
        if (child && child->isCheckable()) {
            child->setCheckState(state);
            updateChildrenCheckState(child, state); // 递归调用
        }
    }
}
